const { dialog } = require("electron").remote;
const fs = require("fs");
const openBtn = document.getElementById("openBtn");
const image = document.getElementById("image");
const saveBtn = document.getElementById("saveBtn");
const notifyBtn = document.getElementById("notifyBtn");
const btn_openMultiFile = document.getElementById("openMultiFile");
const path = require("path");
openBtn.onclick = () => {
  dialog
    .showOpenDialog({
      title: "Choose your picture",
      defaultPath: "timg.jpg",
      filters: [
        {
          name: "image",
          extensions: ["jpg", "png", "jpe"],
        },
        {
          name: "png",
          extensions: ["png"],
        },
      ],
      buttonLabel: "打开图片xxx",
    })
    .then((result) => {
      image.setAttribute("src", result.filePaths[0]);
    })
    .catch((err) => {
      console.error(err);
    });
};

saveBtn.onclick = () => {
  dialog
    .showSaveDialog({
      title: "保存",
      buttonLabel: "保存xxxx",
    })
    .then((result) => {
      fs.writeFileSync(result.filePath, "12345.com");
    })
    .catch((err) => {
      console.error(err);
    });
};
notifyBtn.onclick = () => {
  console.log("xxxx");
  const option = {
    title: "这是标题",
    body: "这是消息",
  };
  const notify = new window.Notification(option.title, option);
  console.log(notify);
  notify.onclick = () => {
    console.log("notify click");
  };
};

btn_openMultiFile.onclick = () => {
  console.log("xxxx");
  const filters = [
    [
      { name: "pdf", extensions: ["pdf"] },
      { name: "Images", extensions: ["jpg", "png", "gif"] },
      { name: "Movies", extensions: ["mkv", "avi", "mp4"] },
      { name: "Custom File Type", extensions: ["as"] },
      { name: "All Files", extensions: ["*"] },
    ],
  ];
  const properties = ["openFile", "openDirectory", "multiSelections"];
  const options = {
    title: "这是标题",
    defaultPath: path.resolve("./"),
    filters,
    properties,
  };
  dialog
    .showOpenDialog(options)
    .then((result) => {
      console.log(result.canceled);
      console.log(result.filePaths);
    })
    .catch((err) => {
      console.log(err);
    });
};
